package com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.mp3.dwonloader.ringtonemaker.musicradio.zl.R;
import com.tools.ringtone.adapter.MusicFileAdapter;
import com.tools.ringtone.data.MusicFileItem;
import com.tools.ringtone.util.MusicFileManager;
import com.tools.ringtone.view.ActionTypeDialog;

public class Start extends Activity implements OnItemClickListener,
		OnClickListener {
	/** Called when the activity is first created. */
	private ListView listViewFile;
	private Button recordButton;
	private MusicFileAdapter mMusicFileAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_ringtone);
		bundleContentView();
		initData();
		
		
		
//		final LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
//		final AdView adView = new AdView(this, AdSize.BANNER, "a14e3cab66eea7a");
//		layout.addView(adView);
//		adView.loadAd(new AdRequest());
	}

	private void bundleContentView() {
		listViewFile = (ListView) findViewById(R.id.list);
		recordButton = (Button) findViewById(R.id.record);
	}

	private void initData() {
		final Cursor cursor = MusicFileManager.createCursorByPath(this, "");
		if (cursor == null) {
			Toast.makeText(this,
					"There are no media can be found in your phone",
					Toast.LENGTH_LONG).show();
			return;
		}
		startManagingCursor(cursor);
		// no need else.
		if (cursor.getCount() == 0) {
			Toast.makeText(this,
					"There are no media can be found in your phone",
					Toast.LENGTH_LONG).show();
		}
		mMusicFileAdapter = new MusicFileAdapter(this, cursor);
		listViewFile.setDivider(getResources().getDrawable(
				R.drawable.ic_divider_ringtone));
		listViewFile.setCacheColorHint(Color.TRANSPARENT);
		listViewFile.setAdapter(mMusicFileAdapter);
		listViewFile.setOnItemClickListener(this);
		recordButton.setOnClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final MusicFileItem audioItem = (MusicFileItem) view.findViewById(
				R.id.image_icon).getTag();
		final ActionTypeDialog actionTypeDialog = new ActionTypeDialog(Start.this,
				audioItem);
		actionTypeDialog.show();
	}

	@Override
	public boolean onKeyDown(int arg0, KeyEvent arg1) {
		if (arg0 == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this)
					.setTitle(getString(R.string.app_name))
					.setMessage(getString(R.string.dialog_sure_exit))
					.setPositiveButton(getString(R.string.dialog_confirm),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									finish();
								}
							})
					.setNegativeButton(getString(R.string.dialog_cancel),
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {

								}
							}).show();
			return true;
		}
		return super.onKeyDown(arg0, arg1);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.record) {
			onRecord();
		}
	}

	// Result codes
	private static final int REQUEST_CODE_EDIT = 1;

	/**
	 * 
	 */
	private void onRecord() {
		try {
			final Intent intent = new Intent(Intent.ACTION_EDIT, Uri.parse("record"));
			// intent.putExtra("was_get_content_intent", mWasGetContentIntent);
			intent.setClassName(
					"com.mp3.dwonloader.ringtonemaker.musicradio.version.zl",
					"com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.activity.AudioEditActivity");
			startActivityForResult(intent, REQUEST_CODE_EDIT);
		} catch (final Exception e) {
		}
	}

}